// 专门处理权限路由
import { constantRoutes, asyncRoutes } from '@/router'

const state = {
	routes: constantRoutes // 路由表 表示当前用户所拥有的的路由的数组
}

const mutations = {
	setRoutes (state, newRoutes) {
		state.routes = [...constantRoutes, ...newRoutes]
	}
}

const actions = {
	// 筛选路由
	// 第二个参数为当前用户所拥有的菜单全选
	filterRoutes (context, menus) {
		const routes = []
		menus.forEach(key => {
			const newRoutes = asyncRoutes.filter(item => item.name === key)
			routes.push(...newRoutes)
		})

		context.commit('setRoutes', routes) // 将动态路由提交给mutatios
		return routes // 此处return是为了显示左侧菜单用的
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
